<?php
	class GeoMapHelper extends AppHelper
	{
		var $helpers = array("Html",'Javascript');
		function getInfobxoHtml($event)
		{
			uses("flay");
			$disableEditorReviews = cr('GeneralConfig.disable_editor_reviews');
			$userReviewCount = '<div style="float:left;margin-left:80px">('.$event['Event']['comment_count'].')</div>';
			$event['Event']['title'] = ucwords(Flay::fragment($event['Event']['title'],10));
			if (isset($this->params['admin']) && $this->params['admin']==1)
				$event['Event']['viewUrl'] = $this->Html->url($event['Event']['viewUrlAdmin']);
			else
				$event['Event']['viewUrl'] = $this->Html->url($event['Event']['viewUrl']);
			
			$event['Event']['when'] = substr($event['Event']['event_from'],8,2)."/".substr($event['Event']['event_from'],5,2)."&nbsp;".substr($event['Event']['event_from'],11,5)."&nbsp;-&nbsp;".substr($event['Event']['event_to'],8,2)."/".substr($event['Event']['event_to'],5,2)."&nbsp;".substr($event['Event']['event_to'],11,5);
			$event['Event']['category'] = ucwords(Flay::fragment($event['EventCategory']['name'],20));					
			$imageHtml = $this->Html->image($event['Event']['image'],array('thumb'=>cr('Thumbs.geoMap'), 'height'=>cr('Thumbs.geoMap.height'), 'width'=>cr('Thumbs.geoMap.width')));
			//prd(htmlentities($event['Event']['imageHtml']));
			$template = '<div id="event_infobox_:Event.id" class="event_map_info">
				:imageHtml
				<div>
				<div>
					<a href=":Event.viewUrl">:Event.title</a> (:Event.category) <br/>
					:Event.when
				<div>
				<div>
			</div>';
			$eventTemplateArray = Utility::dotNotation($event);			
			$eventHtml = String::insert($template,$eventTemplateArray);
			$eventHtml = String::insert($eventHtml,compact('imageHtml'));
			$eventHtml = "<div class=\"map_content\">".$eventHtml."</div>";
			return $eventHtml;
		}
		function getEventsMarkerJson($events)
		{
			$markers = $this->getEventsMarker($events);
			$markersJson = $this->Javascript->object($markers);
			return $markersJson;
		}
		function getEventsMarker($events)
		{
			$markers = array();
			foreach($events as $event)
			{
				if(!$event['Event']['lat'] || !$event['Event']['lng'])
				{
					continue;
				}
				$marker['constructOptions']['title'] = $event['Event']['title'];
				//$marker['constructOptions']['address'] = $this->getAddress($event);
				$marker['constructOptions']['address'] = $event['Event']['venue'];	//additional field which has listing address
				$marker['lat'] = $event['Event']['lat'];
				$marker['lng'] = $event['Event']['lng'];
				$marker['id'] = $event['Event']['id'];
				$icon = $this->getIconUrl($event);
				if($icon)
				{
					$marker['constructOptions']['icon'] =	$icon; 
				}
				$marker['info'] = $this->getInfobxoHtml($event);
				$markers[] = $marker;
			}
			return $markers;
		}
		
		private function getAddress($event)
		{
			$address = "";
			$fields = array('address1_field','address2_field','state','city','postal_code','country');
			foreach($fields as $field)
			{
				$eventFieldname = cr("MapConfig.".$field);
				if($eventFieldname && isset($event['Event'][$eventFieldname]))
				{
					$address .= ' '.$event['Event'][$eventFieldname];
				}
			}
			return $address;
		}
		
		private function getIconUrl($event)
		{
			if(!isset($this->categoryIcons))
			{
				$this->categoryIcons = cri("MapIcon")->getCategoryIcons();
			}
			$CategoryModel = cri("EventCategory");
			$icon = null;
			$iconUrl = null;
			$categoryId = $event['Event']['event_category_id'];
			if(isset($this->categoryIcons[$categoryId]) && $this->categoryIcons[$categoryId])
			{
				$icon = $this->categoryIcons[$categoryId];
			}
			if($icon)
			{
				$iconUrl = cr("cmsConfig.Backend.siteUrl").'/geo_images/'.$icon;
			}
			return $iconUrl;
		}
}
?>